package sword;

/**
 * @Author: 海琳琦
 * @Date: 2022/5/30 11:58
 * https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/
 */
public class Title04 {

    boolean flag;
    int n, m;

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        n = matrix.length;
        if (n == 0) {
            return false;
        }
        m = matrix[0].length;
        if (m == 0) {
            return false;
        }
        flag = false;
        int[][] visited = new int[n][m];
        dfs(matrix, target, 0, 0, visited);
        return flag;
    }

    private void dfs(int[][] matrix, int target, int i, int j, int[][] visited) {
        if (i >= n || j >= m || visited[i][j] == 1 || matrix[i][j] > target) {
            return;
        }
        if (matrix[i][j] == target) {
            flag = true;
            return;
        }
        visited[i][j] = 1;
        int[] dx = {1, 0};
        int[] dy = {0, 1};
        for (int k = 0; k < 2; k++) {
            dfs(matrix, target, i + dx[k], j + dy[k], visited);
        }
    }

    public boolean findNumberIn2DArray1(int[][] matrix, int target) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int n = matrix.length;
        int m = matrix[0].length;
        int row = 0;
        int column = m - 1;
        while (row < n && column >= 0) {
            int num = matrix[row][column];
            if (num > target) {
                column--;
            } else if (num < target) {
                row++;
            }else{
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] a = {};
        System.out.println(a.length);
    }
}
